﻿using Kinlo.MESDocking;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using UIWindows.GlobalStatics;
using UIWindows.Models.Devices;
using UIWindows.Models.ParameterModels;
using XingLucifer.Devices;
using XingLucifer.IBase.Enums;
using UIWindows.Tools.Helper;
using System.Reflection;
using Kinlo.Entitys;
using NewLife.Data;
using MathNet.Numerics;
using XCode;
using System.Threading;
using Newtonsoft.Json;
using UIWindows.MESModels;
using UIWindows.Views.DeviceInteractiveaddressItems;
using NewLife.Reflection;
using System.Diagnostics;
using System.Reflection.PortableExecutable;
using UIWindows.Models;
using Kinlo.MESDocking.MQTT.MQTTMessModels;

namespace UIWindows.Services.PLC_MESHelper
{
    /// <summary>
    /// 工艺参数采集类
    /// </summary>
    public class ProcessParameterHelper : HelperBase
    {

        PLCBase InjMachine { get; set; }
        //MESCollectionItemsParameterModel parameter { get; set; }

        MESParmLimit _mesParmLimit { get; set; }

        Stopwatch _processStopwatch = new Stopwatch();


        public ProcessParameterHelper(
            DeviceInteractiveaddressStatic deviceInteractiveaddressStatic,
            DeviceClientModel deviceModel,
            TemporaryStatic temporaryStatic,
            ParameterStatic parameterStatic,
            DeviceConfigStatic deviceConfigStatic,
            SystemConfigStatic systemConfigStatic,
             HomeStatic homeStatic,
             Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog
            ) : base(deviceInteractiveaddressStatic, deviceModel, temporaryStatic, parameterStatic, deviceConfigStatic, systemConfigStatic, homeStatic, log, dialog)
        {
            InjMachine =  deviceModel.PLC;
            //parameter = _parameterStatic.MESCollectionItemsParameter[(MachineInfoTypes)Enum.Parse(typeof(MachineInfoTypes), deviceModel.ServiceName)];
            _mesParmLimit = new MESParmLimit(_log, _dialog, machineInfo_Type);

            var isResultList = _collectParamete != null ? _collectParamete.FindAll(x => x.IsCCDResult == true).ToList():new List<CollectParameterModel>();
            if(isResultList==null|| isResultList.Count==0)
            {
                _processStopwatch.Start();
            }
        }
        public override Task Handle()
        {
            ProcessParameterData();
            return Task.CompletedTask;
        }
        /// <summary>
        /// 工艺参数采集保存到本地数据库
        /// </summary>
        public void ProcessParameterData()
        {
            //ProductionData.Meta.TableName = $"productiondata_20233";

            //ProductionData aa = new ProductionData();

            ////aa.Insert();

            ////Type entityType = typeof(ProductionData);
            //// 获取 FindAll 方法
            //var findAllMethod = typeof(ProductionData).GetMethods();
            //foreach(var iitem in findAllMethod)
            //{
            //    Debug.WriteLine(iitem.Name);
            //}
            //// 获取方法
            //MethodInfo method11 = typeof(ProductionData).GetMethod("FindAll", BindingFlags.Static | BindingFlags.Public, null,
            //                new Type[] { typeof(Expression), typeof(String), typeof(String), typeof(Int64), typeof(Int64) }, null);


            //MethodInfo productionMethod = typeof(ProductionData).GetMethod("FindAll", BindingFlags.Static | BindingFlags.Public, null, 
            //    new Type[] { typeof(Expression), typeof(String), typeof(String), typeof(Int64), typeof(Int64) }, null);


            //object result55 = productionMethod.Invoke(null, new object[] { null, "SnowflakeId DESC", null, 0, 1 });
            try
            {
               if (machineInfo_Type==MachineInfoTypes.卷芯上料|| machineInfo_Type.ToString().Contains("装盘机") 
                    || machineInfo_Type.ToString().Contains("拆盘机")|| machineInfo_Type == MachineInfoTypes.预充装盘)
                {
                    return;
                }

                if (_collectParamete == null || _collectParamete.Count == 0)
                {
                    return;
                }

                var isResultList = _collectParamete!=null? _collectParamete.FindAll(x => x.IsCCDResult == true).ToList()
                    :new List<CollectParameterModel>();

                    List<CollectParameterModel> isTrueResultList = new List<CollectParameterModel>();
                    int isCCDResult = 0;
                    foreach (var isResult in isResultList)
                    {
                        string value = ProcessList(InjMachine.Read(isResult.PLCAddress, isResult.PLCAddressLength),0);
                        if (value == "1" || value == "2")
                        {
                            isTrueResultList.Add(isResult);
                            isCCDResult ++;
                        }
                    }

                    if (isCCDResult < isResultList.Count()) return;
                //if (isCCDResult == 0) return;

                if (isCCDResult == 0 && (isResultList == null || isResultList.Count() == 0))
                {
                    var addReassData = GetAddReassData("MachineInCountAddress");
                    var InCounst = InjMachine.ReadUInt32(addReassData.PLCAddress, 1);
                    addReassData = GetAddReassData("MachineOkCountAddress");
                    var OkCounst = InjMachine.ReadUInt32(addReassData.PLCAddress, 1);

                    if (((InCounst != null && _parameter.PutIntoNumber == (int)InCounst[0])
                        && (OkCounst != null && _parameter.QualifiedNumber == (int)OkCounst[0])
                            )|| _processStopwatch.Elapsed.TotalMinutes<5)
                        {
                            return;
                        }
                    }

                    var entityType = GetEntityType();
                    var entity = CreateEntityType<object>();


                    //PropertyInfo propertyInData = entity.GetType().GetProperty("Operator");
                    //string valueToCompare = propertyInData.GetValue(entity)?.ToString();   // 假设你要比较的值

                    // 设置数据库表名
                    PropertyInfo tableNameProperty = entityType.Item2.GetProperty("TableName", BindingFlags.Static | BindingFlags.Public);
                    tableNameProperty.SetValue(entity, Convert.ChangeType($"{entityType.Item1.Name}_{DateTime.Now:yyyy}{DateTime.Now.GetQuarterly()}", tableNameProperty.PropertyType), null);


                    //PropertyInfo idProperty = entity.GetType().GetProperty("Id");

                    //雪花id
                    //long snowflake = _snowflake.NextId();
                    //idProperty.SetValue(entity, Convert.ChangeType(snowflake, idProperty.PropertyType), null);

                    //ProductionData.Meta.TableName = $"ProductionData_{DateTime.Now:yyyy}{DateTime.Now.GetQuarterly()}";

                    #region 采集工艺参数
                    ProcessParameters(entity,InjMachine);
                    //foreach (var item in _collectParamete)
                    //{

                    //    var valueType = GetTypeByStringHelper.GetTypeByString(item.DataType);
                    //    var sss = entity.GetType();
                    //    var entityInfo = entity.GetType().GetProperty(item.PropertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    //    if (entityInfo != null)
                    //    {
                    //        try
                    //        {
                    //            string value = "0";
                    //            if (item.IsEnable)
                    //            {

                    //                if (item.IsCCDResult)
                    //                {
                    //                    value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), 0);
                    //                }
                    //                else
                    //                {
                    //                    if (item.DataType.Contains("int32"))
                    //                    {
                    //                        var valueData = InjMachine.ReadUInt32(item.PLCAddress, 1);
                    //                        value = valueData[0].ToString();
                    //                    }
                    //                    else
                    //                    {
                    //                        value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), item.DecimalNum);
                    //                    }

                    //                }
                    //            }

                    //            //赋值
                    //            entityInfo.SetValue(entity, Convert.ChangeType(value, entityInfo.PropertyType), null);
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            Console.WriteLine(ex.ToString());
                    //        }

                    //    }
                    //}
                    #endregion

                    #region 读取焊机数据
                    ProcessSpotWelderData(entity);
                    //  var spotWelderDevice = _deviceConfig.DeviceList.Where(x => x.ServiceName == _deviceModel.ServiceName
                    //&& x.TaskType == TaskTypes.点底焊机).ToList();
                    //  if (spotWelderDevice != null && spotWelderDevice.Count > 0)
                    //  {
                    //      var spotWelderDataList = new List<SpotWelderData>();
                    //      foreach (var item in spotWelderDevice)
                    //      {
                    //          var spotWelderDatas = item.Communication.Read();
                    //          if (spotWelderDatas != null)
                    //          {
                    //              var data = ParseSpotWelderData(spotWelderDatas); //解析数据
                    //              if (data != null)
                    //              {
                    //                  spotWelderDataList.Add(data);
                    //              }
                    //          }
                    //      }

                    //      var parameters = new Dictionary<string, string>
                    //      {
                    //          { "DiandiParameters9", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.VoltagePeak ?? "0" : "0" },
                    //          { "DiandiParameters10", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.VoltageAverage ?? "0" : "0" },
                    //          { "DiandiParameters11", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.CurrentPeak ?? "0" : "0" },
                    //          { "DiandiParameters12", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.CurrentAverage ?? "0" : "0" },
                    //          { "DiandiParameters13", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.Power ?? "0" : "0" },
                    //          { "DiandiParameters14", spotWelderDataList.Count > 0 ? spotWelderDataList[0]?.Impedance ?? "0" : "0" },
                    //          { "DiandiParameters28", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.VoltagePeak ?? "0" : "0" },
                    //          { "DiandiParameters29", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.VoltageAverage ?? "0" : "0" },
                    //          { "DiandiParameters30", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.CurrentPeak ?? "0" : "0" },
                    //          { "DiandiParameters31", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.CurrentAverage ?? "0" : "0" },
                    //          { "DiandiParameters32", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.Power ?? "0" : "0" },
                    //          { "DiandiParameters33", spotWelderDataList.Count > 1 ? spotWelderDataList[1]?.Impedance ?? "0" : "0" },
                    //          { "DiandiParameters34", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.VoltagePeak ?? "0" : "0" },
                    //          { "DiandiParameters35", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.VoltageAverage ?? "0" : "0" },
                    //          { "DiandiParameters36", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.CurrentPeak ?? "0" : "0" },
                    //          { "DiandiParameters37", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.CurrentAverage ?? "0" : "0" },
                    //          { "DiandiParameters38", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.Power ?? "0" : "0" },
                    //          { "DiandiParameters39", spotWelderDataList.Count > 2 ? spotWelderDataList[2]?.Impedance ?? "0" : "0" },
                    //          { "DiandiParameters40", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.VoltagePeak ?? "0" : "0" },
                    //          { "DiandiParameters41", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.VoltageAverage ?? "0" : "0" },
                    //          { "DiandiParameters42", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.CurrentPeak ?? "0" : "0" },
                    //          { "DiandiParameters43", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.CurrentAverage ?? "0" : "0" },
                    //          { "DiandiParameters44", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.Power ?? "0" : "0" },
                    //          { "DiandiParameters45", spotWelderDataList.Count > 3 ? spotWelderDataList[3]?.Impedance ?? "0" : "0" }
                    //      };

                    //      foreach (var parameter in parameters)
                    //      {
                    //          var entityInfo = entity.GetType().GetProperty(parameter.Key, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    //          if (entityInfo != null)
                    //          {
                    //              entityInfo.SetValue(entity, Convert.ChangeType(parameter.Value, entityInfo.PropertyType), null);
                    //          }
                    //      }
                    //  }

                    #endregion

                    #region 读取短路测试仪数据
                    ProcesscircuitTester(entity);
                    //  var circuitTesterDeviceList = _deviceConfig.DeviceList.Where(x => x.ServiceName == _deviceModel.ServiceName
                    //&& x.TaskType == TaskTypes.短路测).ToList();

                    //  if (circuitTesterDeviceList != null && circuitTesterDeviceList.Count > 0)
                    //  {
                    //      var circuitTesterDataList = new List<CircuitTesterData>();
                    //      foreach (var item in circuitTesterDeviceList)
                    //      {
                    //          var circuitTesteDatas = item.Communication.CircuitTesterReceive();
                    //          if (circuitTesteDatas != null)
                    //          {
                    //              var IR = circuitTesteDatas["IR"].Replace("\n", "");
                    //              var Current = circuitTesteDatas["Current"].Replace("\n", "");
                    //              var Voltage = circuitTesteDatas["Voltage"].Replace("\n", "");
                    //              var TestDatetime = DateTime.Now.ToString().Replace("\n", "");
                    //              circuitTesterDataList.Add(new CircuitTesterData
                    //              {
                    //                  IR = IR.Length >= 5 ? IR.Substring(0, 5) : IR.PadRight(5, '0'),
                    //                  Current = Current.Length >= 5 ? Current.Substring(0, 5) : Current.PadRight(5, '0'),
                    //                  Voltage = Voltage.Length >= 5 ? Voltage.Substring(0, 5) : Voltage.PadRight(5, '0'),
                    //                  TestDatetime = TestDatetime
                    //              });
                    //          }
                    //      }

                    //      var parameters = new Dictionary<string, string>
                    //      {
                    //          { "GuncaoParameters20", circuitTesterDataList.Count > 0 ? circuitTesterDataList[0]?.IR ?? "0" : "0" },
                    //          { "GuncaoParameters21", circuitTesterDataList.Count > 0 ? circuitTesterDataList[0]?.Voltage ?? "0" : "0" },
                    //          { "GuncaoParameters22", circuitTesterDataList.Count > 0 ? circuitTesterDataList[0]?.Current ?? "0" : "0" },
                    //          { "GuncaoParameters24", circuitTesterDataList.Count > 0 ? circuitTesterDataList[0]?.Current ?? "0" : "0" },
                    //          { "GuncaoParameters25", circuitTesterDataList.Count > 0 ? circuitTesterDataList[0]?.Voltage ?? "0" : "0" },
                    //          { "GuncaoParameters23", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                    //          { "GuncaoParameters29", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                    //          { "GuncaoParameters26", circuitTesterDataList.Count > 1 ? circuitTesterDataList[1]?.IR ?? "0" : "0" },
                    //          { "GuncaoParameters27", circuitTesterDataList.Count > 1 ? circuitTesterDataList[1]?.Voltage ?? "0" : "0" },
                    //          { "GuncaoParameters28", circuitTesterDataList.Count > 1 ? circuitTesterDataList[1]?.Current ?? "0" : "0" },
                    //          { "GuncaoParameters30", circuitTesterDataList.Count > 1 ? circuitTesterDataList[1]?.Current ?? "0" : "0" },
                    //          { "GuncaoParameters31", circuitTesterDataList.Count > 1 ? circuitTesterDataList[1]?.Voltage ?? "0" : "0" },
                    //      };

                    //      foreach (var parameter in parameters)
                    //      {
                    //          var entityInfo = entity.GetType().GetProperty(parameter.Key, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    //          if (entityInfo != null)
                    //          {
                    //              entityInfo.SetValue(entity, Convert.ChangeType(parameter.Value, entityInfo.PropertyType), null);
                    //          }
                    //      }
                    //}

                    #endregion

                    #region 读取白班OR夜班NG分类

                    ProcessClassNGType(entity, InjMachine);
                    //if (_mesRunParameters1 != null && _mesRunParameters2 != null)
                    //{
                    //    //判断是否白班
                    //    if(_parameter.Class.Contains("1"))
                    //    {
                    //        foreach (var item in _mesRunParameters1)
                    //        {
                    //            if (!item.IsEnable) continue;
                    //            var valueType = GetTypeByStringHelper.GetTypeByString(item.DataType);
                    //            var sss = entity.GetType();
                    //            var entityInfo = entity.GetType().GetProperty(item.PropertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    //            if (entityInfo != null)
                    //            {
                    //                try
                    //                {
                    //                    string value = "";
                    //                    if (item.IsCCDResult)
                    //                    {
                    //                        value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), 0);
                    //                    }
                    //                    else
                    //                    {
                    //                        if (item.DataType.Contains("int32"))
                    //                        {
                    //                            value = InjMachine.ReadUInt32(item.PLCAddress, 1)[0].ToString();
                    //                        }
                    //                        else
                    //                        {
                    //                            value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), item.DecimalNum);
                    //                        }
                    //                    }
                    //                    //赋值
                    //                    entityInfo.SetValue(entity, Convert.ChangeType(value, entityInfo.PropertyType), null);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    Console.WriteLine(ex.ToString());
                    //                }

                    //            }
                    //        }
                    //    }
                    //    else
                    //    {
                    //        foreach (var item in _mesRunParameters2)
                    //        {
                    //            if (!item.IsEnable) continue;
                    //            var valueType = GetTypeByStringHelper.GetTypeByString(item.DataType);
                    //            var sss = entity.GetType();
                    //            var entityInfo = entity.GetType().GetProperty(item.PropertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    //            if (entityInfo != null)
                    //            {
                    //                try
                    //                {
                    //                    string value = "";
                    //                    if (item.IsCCDResult)
                    //                    {
                    //                        value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), 0);
                    //                    }
                    //                    else
                    //                    {
                    //                        value = ProcessList(InjMachine.Read(item.PLCAddress, item.PLCAddressLength), item.DecimalNum);
                    //                    }
                    //                    //赋值
                    //                    entityInfo.SetValue(entity, Convert.ChangeType(value, entityInfo.PropertyType), null);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    Console.WriteLine(ex.ToString());
                    //                }

                    //            }
                    //        }
                    //    }



                    //}

                    #endregion

                    try
                    {
                        //插入数据方法
                        MethodInfo method = entityType.Item1.GetMethod("Insert");
                        object result = method.Invoke(entity, null);
                    }
                    catch (Exception ex)
                    {
                        RunLog($"工艺参数插入数据库失败！！！{ex.ToString()}");
                        if (ex.InnerException != null)
                        {
                            RunLog($"内部异常：{ex.InnerException.ToString()}");
                        }
                    }
                    //给CCD写入结果，说明已经取过这条数据
                    foreach (var CCDItem in isTrueResultList)
                    {
                        PropertyInfo ccdProperty = entity.GetType().GetProperty(CCDItem.PropertyName);
                        string ccdValue = ccdProperty.GetValue(entity)?.ToString();

                        InjMachine.Write(CCDItem.PLCAddress, new short[] { (short)(int.Parse(ccdValue) * 10) });
                    }

                    CompareStandardProcess(entity);
                    MachineInfoTypes machineInfoType = (MachineInfoTypes)Enum.Parse(typeof(MachineInfoTypes), _deviceModel.ServiceName);
                    _homeStatic.AddBattery(entity, machineInfoType);

                if(_processStopwatch.Elapsed.TotalMinutes >= 5)
                {
                    _processStopwatch.Restart();
                }
                //}
            }catch(Exception ex)
            {
                RunLog($"工艺参数采集错误！！！{ex.ToString()}");
            }
           
        }


        /// <summary>
        /// 标准参数对比接口
        /// </summary>
        public void CompareStandardProcess(object entity)
        {
            try
            {
                //当前采集工艺参数对比标准工艺参数
                if (_parameter.GeneralData.Count > 0)
                {

                    Dictionary<string, object> mesParams = new Dictionary<string, object>
                            {
                                { "EquipmentCode", _parameter.EquipmentCode },
                                { "process_id", _parameter.process_id },
                            };
                    Dictionary<string, object> mesParameters = new Dictionary<string, object>
                            {
                                { "parentvo",new Dictionary<string, object>{ { "EquipmentCode", _parameter.EquipmentCode.ToString() } } },
                            };
                    List<Dictionary<string, object>> childrenvo = new List<Dictionary<string, object>>();

                    var generalData = _parameter.GeneralData;
                    foreach (var item in generalData)
                    {

                        string propertyName = item.ParameterCode.Replace("_", ""); // 获取参数编码作为属性名

                        //PropertyInfo propertyInData = entity.GetType().GetProperty(propertyName); // 在中工艺参数中查找属性
                        // 在中工艺参数中查找属性，忽略大小写
                        PropertyInfo propertyInData = entity.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                        if (propertyInData != null)
                        {
                            string valueToCompare = propertyInData.GetValue(entity)?.ToString();   // 假设你要比较的值

                            if (string.IsNullOrWhiteSpace(valueToCompare))
                            {
                                continue;
                            }
                            double value = double.Parse(valueToCompare);        //转成双精度小数

                            if (value <= 0)
                            {
                                continue;
                            }

                            bool isNg = false;
                            if (!string.IsNullOrEmpty(item.UpperValue) && !string.IsNullOrEmpty(item.LowerValue))
                            {
                                // 既有上限值又有下限值的情况
                                double upper = double.Parse(item.UpperValue);
                                double lower = double.Parse(item.LowerValue);


                                if (value < lower || value > upper)
                                {
                                    isNg = true;

                                    //$"[{_data.MachineName}]MES参数{item.ParameterName}不在上下限范围内,\r\nMES上限值：{upper}，MES下限值：{lower}，生产值：{value}".Info();
                                }
                                //Console.WriteLine("不在上下限范围内");
                            }
                            else if (!string.IsNullOrEmpty(item.LowerValue))
                            {
                                // 只有下限值的情况
                                double lower = double.Parse(item.LowerValue);

                                if (value < lower)
                                {
                                    isNg = true;
                                    //$"[{_data.MachineName}]MES参数{item.ParameterName}低于下限, MES下限值：{lower}，生产值：{value}".Info();
                                }
                                //Console.WriteLine("低于下限");
                            }
                            else if (!string.IsNullOrEmpty(item.UpperValue))
                            {
                                // 只有上限值的情况
                                double upper = double.Parse(item.UpperValue);

                                if (value > upper)
                                {
                                    isNg = true;
                                    //$"[{_data.MachineName}]MES参数{item.ParameterName}超出上限,MES上限值：{upper}，生产值：{value}".Info();
                                }
                                //Console.WriteLine("超出上限");

                            }
                            else if (!string.IsNullOrEmpty(item.StandardValue))
                            {
                                // 只有标准值的情况
                                double standard = double.Parse(item.StandardValue);

                                if (value != standard)
                                {
                                    isNg = true;
                                    //$"[{_data.MachineName}]MES参数{item.ParameterName}不等于标准值,MES标准值：{standard}，生产值：{value}".Info();
                                }
                            }
                            if (isNg)
                            {
                                childrenvo.Add(new Dictionary<string, object>
                                {
                                    {"parameterName",  item.ParameterName},
                                    {"parameterCode",  item.ParameterCode},
                                    {"ActualValue", value.ToString()},
                                    {"standardValue",  item.StandardValue},
                                    {"upperValue",  item.UpperValue},
                                    {"lowerValue",  item.LowerValue},
                                    {"CreatTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
                                    {"StepCode", item.StepCode},
                                    {"StepName", item.StepName},
                                });
                            }

                        }                     
                    }
                    if (childrenvo != null && childrenvo.Count > 0)
                    {
                        mesParameters.Add("childrenvo", childrenvo);

                        mesParams.Add("parameter", mesParameters);

                        var result = _mesParmLimit.MESInteraction(_parameterStatic.MESParameter.ParmLimitRoute,
                             _parameterStatic.MESParameter.ParmLimitTime,
                             1, DateTime.Now, mesParams);
                    }
                     

                }
            }catch (Exception ex)
            {
                RunLog($"标准工艺参数对比报错：{ex}");
            }
         
        }
    }
}
